﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Autenticacao
{
    public partial class frm_ianimal1 : Form
    {
        System.Data.SqlClient.SqlConnection con;
        
        public Cliente cli1;
        public Utilizador util1;
        
        public frm_ianimal1(Utilizador util)
        {
            this.util1 = util;
            InitializeComponent();

            this.dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
        }

        private void pesquisar_cliente()
        {
            con = new System.Data.SqlClient.SqlConnection();
            con.ConnectionString = @"Data Source = ANDRÉ-PC\SQLEXPRESS; Initial Catalog = horsepw_bd; Integrated Security = True;";
            con.Open();

            int id = util1.get_id_vet();
            SqlCommand command1 = con.CreateCommand();
            command1.CommandText = "SELECT id_cliente, nome, morada, cod_postal, localidade, telemovel, email FROM Cliente WHERE nome LIKE '%" + txtgc_nome.Text + "%' and id_veterinario = " + id + " and oculto = 0";
            SqlDataReader readatrib = command1.ExecuteReader();

            this.dataGridView1.Rows.Clear();

            while (readatrib.Read())
            {
                int i = this.dataGridView1.Rows.Add();

                this.dataGridView1[0, i].Value = readatrib["id_cliente"].ToString();
                this.dataGridView1[1, i].Value = readatrib["nome"].ToString();
                this.dataGridView1[2, i].Value = readatrib["morada"].ToString();
                this.dataGridView1[3, i].Value = readatrib["cod_postal"].ToString();
                this.dataGridView1[4, i].Value = readatrib["localidade"].ToString();
                this.dataGridView1[5, i].Value = readatrib["telemovel"].ToString();
                this.dataGridView1[6, i].Value = readatrib["email"].ToString();

            }

            con.Close();
        }

        private void cmdia1_pesquisar_Click(object sender, EventArgs e)
        {
            try
            {
                pesquisar_cliente();
            }
            catch (Exception er)
            {
                MessageBox.Show(er.ToString());
            }
        }

        private void cmdia1_continuar_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.RowCount != 0)
            {
                con = new System.Data.SqlClient.SqlConnection();
                con.ConnectionString = @"Data Source = ANDRÉ-PC\SQLEXPRESS; Initial Catalog = horsepw_bd; Integrated Security = True;";
                con.Open();

                SqlCommand command1 = con.CreateCommand();
                command1.CommandText = "SELECT MAX(id_animal) FROM Animais";
                SqlCommand command2 = con.CreateCommand();
                command2.CommandText = "SELECT Count(*) FROM Animais";
                int num1 = (Int32)command2.ExecuteScalar();
                int num = 0;
                if (num1 == 0)
                    num = 1;
                else
                {
                    num = (Int32)command1.ExecuteScalar();
                    num++;
                }
                con.Close();

                Cliente m = new Cliente(Convert.ToInt32(dataGridView1["id_cliente", dataGridView1.CurrentRow.Index].Value.ToString()), dataGridView1["nome", dataGridView1.CurrentRow.Index].Value.ToString(), dataGridView1["morada", dataGridView1.CurrentRow.Index].Value.ToString(), Convert.ToInt32(dataGridView1["cod_postal", dataGridView1.CurrentRow.Index].Value.ToString()), dataGridView1["localidade", dataGridView1.CurrentRow.Index].Value.ToString(), Convert.ToInt32(dataGridView1["telemovel", dataGridView1.CurrentRow.Index].Value.ToString()), dataGridView1["email", dataGridView1.CurrentRow.Index].Value.ToString());
                Animal an1 = new Animal(num, "novo nome", "inserir espécie", "inserir nro chip", "AAAA/MM/DD", "M", null, "", "S");

                frm_ianimal frm1 = new frm_ianimal(m, an1, "novo");
                this.Hide();
                frm1.ShowDialog();
            }
            else
                MessageBox.Show("Primeiro pesquise e selecione um cliente!", "Selecione o cliente!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }
    }
}
